//
//  TYHHTTPFacade.m
//  OpenPractice
//
//  Created by tanyuehong on 7/7/18.
//  Copyright © 2018年  lijinghua. All rights reserved.
//

#import "TYHHTTPFacade.h"
#import "UIView+NativeToast.h"
#import <AFNetworking.h>

@implementation TYHHTTPFacade

+ (void)doHttpRequest:(NSString *)url parms:(NSDictionary *)parms completeBlock:(completeBlock)block {
    [self doHttpRequest:url parms:parms headers:nil completeBlock:block];
}

+ (void)doHttpRequest:(NSString *)url parms:(NSDictionary *)parms headers:(NSDictionary *)headers completeBlock:(completeBlock)block {
    if (!url || url.length == 0) {  // 防崩溃
        return ;
    }
    [[self shareSessionManger] POST:url parameters:parms headers:headers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (block&&[responseObject isKindOfClass:[NSDictionary class]]) {
                NSInteger sCode = [[responseObject objectForKey:@"code"] integerValue];
                if(sCode==200) {
                   NSDictionary *dataDict = [responseObject objectForKey:@"data"];
                   block(nil,dataDict);
                } else {
                    NSString *message = [responseObject objectForKey:@"message"];
                    if(!message || message.length == 0 ) {
                        message = @"服务端返回失败，请重试";
                    }
                    NSError *myErr = [NSError errorWithDomain:NSURLErrorDomain code:sCode userInfo:@{NSLocalizedDescriptionKey:message}];
                    block(myErr,nil);
                }
                if ([[responseObject objectForKey:@"err_code"] integerValue]==1) {
                    NSString *toastString = [responseObject objectForKey:@"message"];
                    if (toastString.length>0) {
                        [[UIApplication sharedApplication].keyWindow makeToast:toastString duration:TYHViewToastDration position:CSToastPositionBottom];
                    }
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (block) {
                block(error,nil);
            }
        }];
}

+ (void)doGetHttpRequest:(NSString *)url parms:(NSDictionary *)parms completeBlock:(completeBlock)block {
    [self doHttpRequest:url parms:parms headers:nil completeBlock:block];
}

+ (void)doGetHttpRequest:(NSString *)url parms:(NSDictionary *)parms headers:(NSDictionary *)headers completeBlock:(completeBlock)block {
    if (!url || url.length == 0) {  // 防崩溃
        return ;
    }
    [[self shareSessionManger] GET:url parameters:parms headers:headers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (block&&[responseObject isKindOfClass:[NSDictionary class]]) {
                NSInteger sCode = [[responseObject objectForKey:@"code"] integerValue];
                if(sCode==200) {
                   NSDictionary *dataDict = [responseObject objectForKey:@"data"];
                   if([dataDict isKindOfClass:[NSDictionary class]]) {
                       block(nil,dataDict);
                   } else {
                       NSString *message = [responseObject objectForKey:@"message"];
                       if(!message || message.length == 0 ) {
                           message = @"服务端返回数据格式异常";
                       }
                       NSError *myErr = [NSError errorWithDomain:NSURLErrorDomain code:sCode userInfo:@{NSLocalizedDescriptionKey:message}];
                       block(myErr,nil);
                   }
                } else {
                    NSString *message = [responseObject objectForKey:@"message"];
                    if(!message || message.length == 0 ) {
                        message = @"服务端返回失败，请重试";
                    }
                    NSError *myErr = [NSError errorWithDomain:NSURLErrorDomain code:sCode userInfo:@{NSLocalizedDescriptionKey:message}];
                    block(myErr,nil);
                }
                if ([[responseObject objectForKey:@"err_code"] integerValue]==1) {
                    NSString *toastString = [responseObject objectForKey:@"message"];
                    if (toastString.length>0) {
                        [[UIApplication sharedApplication].keyWindow makeToast:toastString duration:TYHViewToastDration position:CSToastPositionBottom];
                    }
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (block) {
                block(error,nil);
            }
        }];
}

+ (AFHTTPSessionManager *)shareSessionManger {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    return manager;
}

@end
